package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.media.dtos.WmNewsContentItem;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.entity.WmNewsMaterial;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自媒体图文引用素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-05-25
 */
@Slf4j
@Service
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements WmNewsMaterialService {

    @Autowired
    private WmMaterialService materialService;

    @Override
    public void saveNewsMaterial(WmNewsDto dto) {

        List<WmNewsMaterial> batchInsert = new ArrayList<>();
        //解析正图文
        String content = dto.getContent();
        List<WmNewsContentItem> wmNewsContentItems = JSON.parseArray(content, WmNewsContentItem.class);
        List<WmNewsContentItem> imageItems = wmNewsContentItems.stream().filter(item -> item.equals("image")).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(imageItems)){
            List<WmNewsMaterial> contentImageList = imageItems.stream().map(item -> {
                WmNewsMaterial newsMaterial = new WmNewsMaterial();
                newsMaterial.setMaterialId(item.getId());
                newsMaterial.setOrd(1);
                newsMaterial.setNewsId(dto.getId());
                newsMaterial.setType(0);
                return newsMaterial;
            }).collect(Collectors.toList());
            batchInsert.addAll(contentImageList);
        }
        // 解析封面图
        List<String> coverImageUrls = dto.getImages();
        if(!CollectionUtils.isEmpty(coverImageUrls)){
            for(String coverImageUrl : coverImageUrls){
                WmNewsMaterial newsMaterial = new WmNewsMaterial();
                WmMaterial material = materialService.getByUrl(coverImageUrl);
                if(material == null){
                    log.error("根据url查询不到素材信息,url:{}", coverImageUrl);
                    throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
                }
                newsMaterial.setMaterialId(material.getId());
                newsMaterial.setOrd(1);
                newsMaterial.setNewsId(dto.getId());
                newsMaterial.setType(0);
                batchInsert.add(newsMaterial);
            }
        }
        // 删除所以旧的关系
        deleteByNewsId(dto.getId());
        // 插入新的关联关系
        if(!CollectionUtils.isEmpty(batchInsert)){
            this.saveBatch(batchInsert);
        }
    }
    private void deleteByNewsId(Integer wmNewsId) {
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getNewsId, wmNewsId);
        this.remove(queryWrapper);
    }
}
